package com.lixunhuan.handacount.controller;


import com.lixunhuan.handacount.common.ResultInfo;
import com.lixunhuan.handacount.db.bean.HandAcount;
import com.lixunhuan.handacount.service.IHandAcountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lixunhuan
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/hand_acount")
public class HandAcountController {
    private static String AUTH_TOKEN = "Authorization";
    @Autowired
    private IHandAcountService handAcountService;

    @RequestMapping("add")
    public ResultInfo addOneHandAcount(HttpServletRequest httpServletRequest,
                                       @RequestParam("type") Integer type,
                                       @RequestParam("kind_id") Integer kindId,
                                       @RequestParam("amount") Double amount,
                                       @RequestParam(value = "desc", required = false) String desc) {
        HandAcount handAcount = new HandAcount();
        handAcount.setUid(httpServletRequest.getHeader(AUTH_TOKEN));
        handAcount.setType(type);
        handAcount.setKindId(kindId);
        handAcount.setAmount(amount);
        if (!StringUtils.isEmpty(desc)) handAcount.setDescstr(desc);
        handAcountService.save(handAcount);
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("添加记账成功");
    }

    @RequestMapping("get_detail")
    public ResultInfo getHandAcountListsByDetail(HttpServletRequest httpServletRequest,
                                                 @RequestParam("type") Integer type) {

        return new ResultInfo(ResultInfo.Status.SUCCESS).result(
                handAcountService.getAcountdetail(httpServletRequest.getHeader(AUTH_TOKEN), type));
    }

}
